<?php

namespace App\Http\Controllers\Manger;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Chanjet\TplusCommonController;
use App\Models\Manger\Manger;
use App\Models\Manger\MangerDepartment;
use App\Services\Chanjet\ChanjetRequest;
use Illuminate\Http\Request;

/**
 * 部门列表
 */
class MangerDepartmentController extends BaseController
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        $status = $request->status ?? 0;
        $isgetall = $request->isgetall ?? 0;
        $list = MangerDepartment::getList($request, $this->mangerinfo['cpid'], $status, $isgetall);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $department_name = $request->department_name;
        $remark = $request->remark ?? '';
        if (!$department_name) {
            return response()->json(['code' => '001', 'msg' => '部门名称不能为空']);
        }
        $isexist = MangerDepartment::where([['is_del', 1], ['department_name', $department_name], ['cpid', $this->mangerinfo['cpid']]])->first();

        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '部门名称不允许重复']);
        }

        $data = [
            'cpid' => $this->mangerinfo['cpid'],
            'department_name' => $department_name,
            'remark' => $remark,
            'update_at' => time()
        ];
        $data['create_at'] = time();
        $mangerId = MangerDepartment::create($data)->id;
        $Chanjet_param = [
            "dto" => [
                "Code" => $mangerId,
                "Name" => $department_name
            ]
        ];
        //增加部门同步
        $requestData = [
            "api" => "departmentCreate",
            "param" => $Chanjet_param,
            "mangerinfo" => $this->mangerinfo
        ];
        \App\Jobs\Manger::dispatch($requestData);
        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $id = $request->id ?? 0;
        $info = MangerDepartment::where([['id', $id], ['cpid', $this->mangerinfo['cpid']], ['status', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }


        $department_name = $request->department_name;
        $remark = $request->remark ?? '';
        if (!$department_name) {
            return response()->json(['code' => '001', 'msg' => '部门名称不能为空']);
        }
        $isexist = MangerDepartment::where([['id', '<>', $id], ['is_del', 1], ['department_name', $department_name], ['cpid', $this->mangerinfo['cpid']]])->first();

        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '部门名称不允许重复']);
        }



        $data = [
            'department_name' => $department_name,
            'remark' => $remark,
            'update_at' => time()
        ];
        MangerDepartment::where([['id', $id], ['cpid', $this->mangerinfo['cpid']]])->update($data);

        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }

    /**
     * 冻结
     */
    public function changestatus(Request $request)
    {

        $id = $request->id ?? 0;
        $info = MangerDepartment::where([['id', $id], ['is_del', 1], ['cpid', $this->mangerinfo['cpid']]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $status = 2;
        MangerDepartment::where([['id', $id]])->update(['status' => $status, 'update_at' => time()]);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }


    public function dodel(Request $request)
    {

        $id = $request->id ?? 0;
        $info = MangerDepartment::where([['id', $id], ['is_del', 1], ['cpid', $this->mangerinfo['cpid']]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $count=Manger::where(['sys_dpm_id'=>$id,'status'=>1])->count();
        if($count>0){
            return response()->json(['code' => '001', 'msg' => '部门下存在管理员，不允许删除']);
        }
        MangerDepartment::where([['id', $id]])->update(['is_del' => 2, 'del_at' => time()]);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
